# Create your views here.
from django.http import Http404,HttpResponse,HttpResponseRedirect
from django.shortcuts import render_to_response,render
from django.core.context_processors import csrf
from django.views.decorators.csrf import csrf_protect
from django.template import RequestContext
from universal import explodePerms,getSig
from django.conf import settings as _s
import urllib2,urllib
from django.utils import simplejson as _j
from api.renren.users import getVisitors
from api.renren import Renren
from api.qqt import QQT
from api.wb import WB
import time


def Bind_login(request):
	_g = request.GET
	if not 'platform' in _g:
		raise Http404
	if not _g['platform']:
		raise Http404
	


def Renren_login(request):
	url=_s.RENREN_AUTH_URL+"grant?client_id="+_s.RENREN_APIKEY+"&scope="+explodePerms(_s.RENREN_PERMS)+"&response_type=code&redirect_uri=http://u.djwong.net:8000/login/renren/auth/?step=token"
	return HttpResponseRedirect(url)

@csrf_protect
def Renren_login_auth(request):
	_g=request.GET
	_r=request.session
	if not 'step' in _g:
		raise Http404()
	if not _g['step']:
		raise Http404()
	if _g['step']=='token':
		if not 'code' in _g:
			raise Http404()
		if not _g['code']:
			raise Http404()
		# url=_s.RENREN_AUTH_URL+"token?grant_type=authorization_code"
# 		url+="&redirect_uri=http://u.djwong.net:8000/login/renren/auth/?step=token"
# 		url+="&client_id="+_s.RENREN_APIKEY
# 		url+="&client_secret="+_s.RENREN_SECRET
# 		url+="&code="+_g['code']
# 		result=(urllib2.urlopen(url)).read()
# 		j_object = _j.loads(result)
# 		result=getVisitors(token=j_object['access_token'])
		r = Renren()
		token = r.getToken(_g['code'],"http://u.djwong.net:8000/login/renren/auth/?step=token")
		r.initAPI()
		result = r.users.getInfo(fields='all')
		return HttpResponse(result)
	else:
		raise Http404()
		
		
def QQT_login(request):
	url=_s.QQT_AUTH_URL+"authorize?response_type=code"
	url+="&client_id="+_s.QQT_APIKEY
	url+="&redirect_uri=http://u.djwong.net:8000/login/qqt/auth/?step=token"
	return HttpResponseRedirect(url)

def QQT_login_auth(request):
	_g=request.GET
	_r=request.session
	if not 'step' in _g:
		raise Http404()
	if not _g['step']:
		raise Http404()
	if _g['step']=='token':
		if not 'code' in _g:
			raise Http404()
		if not _g['code']:
			raise Http404()
		# url=_s.QQT_AUTH_URL+"access_token?grant_type=authorization_code"
# 		url+="&redirect_uri=http://u.djwong.net:8000/login/qqt/auth/?step=token"
# 		url+="&client_id="+_s.QQT_APIKEY
# 		url+="&client_secret="+_s.QQT_SECRET
# 		url+="&code="+_g['code']
# 		result=(urllib2.urlopen(url)).read()
# 		arr1=result.split("&")
# 		token=arr1[0].split("=")[1]
# 		openid=arr1[3].split("=")[1]
# 		ip=get_client_ip(request)
# 		url="https://open.t.qq.com/api/t/list?"
# 		url+="oauth_consumer_key="+_s.QQT_APIKEY
# 		url+="&access_token="+token
# 		url+="&clientip="+ip
# 		url+="&openid="+openid
# 		url+="&oauth_version=2.a"
# 		url+="&format=json"
# 		url+="&ids=202101132130435"
# 		result=(urllib2.urlopen(url)).read()
		q = QQT(get_client_ip(request))
		token = q.getToken(_g['code'],'http://u.djwong.net:8000/login/qqt/auth/?step=token')
		q.initAPI()
		return HttpResponse(q.user.info())
	else:
		raise Http404()
		
def WB_login(request):
	url=_s.WB_AUTH_URL+"authorize?client_id="+_s.WB_APIKEY
	url+="&response_type=code"
	url+="&redirect_uri=http://u.djwong.net:8000/login/wb/auth/?step=token"
	return HttpResponseRedirect(url)

def WB_login_auth(request):
	_g=request.GET
	_r=request.session
	if not 'step' in _g:
		raise Http404()
	if not _g['step']:
		raise Http404()
	if _g['step']=='token':
		if not 'code' in _g:
			raise Http404()
		if not _g['code']:
			raise Http404()
		# url=_s.WB_AUTH_URL+"access_token"
# 		t={}
# 		t['grant_type']='authorization_code'
# 		t['redirect_uri']='http://u.djwong.net:8000/login/wb/auth/?step=token'
# 		t['client_id']=_s.WB_APIKEY
# 		t['client_secret']=_s.WB_SECRET
# 		t['code']=_g['code']
# 		result=(urllib2.urlopen(url,urllib.urlencode(t))).read()
# 		j_object=_j.loads(result)
# 		url="https://api.weibo.com/2/statuses/friends_timeline.json?access_token="+j_object['access_token']
# 		result=(urllib2.urlopen(url)).read()
		wb=WB()
		token=wb.getToken(_g['code'],'http://u.djwong.net:8000/login/wb/auth/?step=token')
		wb.initAPI()
		result=wb.users.show(wb.uid)
		return HttpResponse(result)
	else:
		raise Http404()
		
def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[0]
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip